﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NicoView.Model.Service.Comments
{
    public sealed class SubmitCommentConversions
    {
        public SubmitCommentConversions()
        {
            Conversions = new ObservableCollection<SubmitCommentConversionData>();
        }

        public ObservableCollection<SubmitCommentConversionData> Conversions;

        public void AddDefaultCase()
        {
            this.addRegexReplaceWord("([タﾀ][ヒﾋ])|(死)","ﾀ,ﾋ");
            this.insertCommaToWord("し","ね");
            this.insertCommaToWord("[フふﾌ][ぁァｧ]", "[っッｯ][くクｸ]");
            this.insertCommaToWord("[いイｲ][んンﾝ]", "[もモﾓ][うウｳ]");
            this.insertCommaToWord("[fF][uU]", "[cC][kK]");
            this.insertCommaToWord("((ば)|(バ)|(ﾊﾞ))[イｲい]", "(ブ)|(ﾌﾞ)|(ぶ)");
            this.insertCommaToWord("[ちチﾁ]", "[んンﾝ][こコｺ]");
            this.insertCommaToWord("[sS]", "[eE][xX]");
            this.insertCommaToWord("[せセｾ]", "[っッｯ][くクｸ][すスｽ]");
            this.insertCommaToWord("[あアァ][なナﾅ]", "[るルﾙ]");
            this.insertCommaToWord("[まマﾏ]", "[んンﾝ][こコｺ]");
            this.insertCommaToWord("体", "位");
            this.insertCommaToWord("[ぱパﾊﾟ]", "[んンﾝ][つツﾂ]");
            this.insertCommaToWord("[ぱパﾊﾟ][んンﾝ]", "[てテﾃ][ぃィ]");
            this.insertCommaToWord("カフェ", "ラテ");
            this.insertCommaToWord("フェ", "ラーリ");
            this.insertCommaToWord("処", "女((作)|(航海))");
            this.insertCommaToWord("[ぶブﾌﾞ]", "[すスｽ]");
            this.insertCommaToWord("[かカｶ]", "[すスｽ]");
            this.insertCommaToWord("スレイ", "プニル");

        }

        public void Convert(NicoSendComment comment)
        {
            foreach (var convData in this.Conversions)
            {

                if(convData.IsRegex)
                {
                    Regex regex = new Regex(convData.Key);
                    comment.Body = regex.Replace(comment.Body, convData.Value);
                }else{
                    comment.Body = comment.Body.Replace(convData.Key, convData.Value);
                }
                
            }
        }


        private void insertCommaToWord(string firstParttern, string secondPattern)
        {
            addRegexReplaceWord("(?<first>" + firstParttern + ")(?<second>" + secondPattern + ")", insertCommaFormat);
        }

        private const string insertCommaFormat = "${first},${second}";

       

        private void addRegexReplaceWord(string target, string replace)
        {
            this.Conversions.Add(new SubmitCommentConversionData(target, replace, true));
        }
    }
}
